//
//  ApiManager.swift
//  TeaShop
//
//  Created by 刘泽 on 2018/1/14.
//  Copyright © 2018年 刘泽. All rights reserved.
//

import UIKit
import Moya

var BASE_URL = "http://www.cyjws.com:8098/"

public enum ApiManager {
    //MARK:Get方法
    case GetApi(url:String,dic:[String:Any])
    //MARK:Post方法
    case PostApi(url:String,dic:[String:Any])
    case PostUrlApi(url:String,dic:[String:Any])
    //MARK:Put方法
    case PutApi(url:String,dic:[String:Any])
    case PutUrlApi(url:String,dic:[String:Any])
    //MARK:deleate方法
    case DeleteApi(url:String,dic:[String:Any])
    case DeleteUrlApi(url:String,dic:[String:Any])
    case GetNoParameter(url:String)
    case PostNoParameter(url:String)
}


extension ApiManager: TargetType {

    public var baseURL: URL {
        return URL.init(string: BASE_URL)!

    }
    public var path: String {

        switch self {
            case .GetApi(let url, _),
                 .PostApi(let url, _),
                 .PostUrlApi(let url, _),
                 .GetNoParameter(let url),
                 .PostNoParameter(let url),
                 .PutApi(let url, _),
                 .DeleteApi(let url, _),
                 .PutUrlApi(let url, _),
                 .DeleteUrlApi(let url, _):
            return url
        }

    }
    
    
    public var method: Moya.Method {
        switch self {
        case .GetApi,
             .GetNoParameter:
            return .get
        case .PutApi,
             .PutUrlApi:
            return .put
        case .DeleteApi,
             .DeleteUrlApi:
            return .delete
        default:
            return .post
        }
    }

    public var task: Task {
        switch self {
        case .GetApi( _, let dic),
             .PostUrlApi( _, let dic),
             .PutUrlApi(_, let dic),
             .DeleteUrlApi(_ , let dic):
            return request(dic)
        case .PostApi( _, let dic),
             .PutApi(_, let dic),
             .DeleteApi(_, let dic):
            return bodyRequest(dic)
        default:
            return requestWithoutAddition()
        }

    }
    public var headers: [String : String]? {
        return nil
    }

    
    public var sampleData: Data {
        return "".data(using: String.Encoding.utf8)!
    }

    
}

extension TargetType{
    //基本参数类型
    public func request(_ parameters: [String: Any]?) -> Task {
        return Task.requestParameters(parameters:parameters!, encoding: URLEncoding.default)
    }

    //MARK:body类型
    public func bodyRequest(_ parameters: [String: Any]?) -> Task {
        return Task.requestParameters(parameters:parameters!, encoding: JSONEncoding.default)
    }

    //MARK:没有参数
    public func requestWithoutAddition() -> Task{
        return Task.requestPlain
    }

    //MARK:上传
    public func uploadData(mulData:[MultipartFormData]) -> Task{
        return Task.uploadMultipart(mulData)
    }
}
